﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FCNS.KeepMoney.Util;

namespace FCNS.KeepMoney.Forms
{
    public partial class CalcForm : ParentForm
    {
        public void caculate(string num)
        {
            if (prestr == "0")
            {
                if (num != ".")
                    return;
            }

            lastchar = null;

            alreq = null;
            if (iseq == "eq")
            {
                iseq = null;
                if (alreq != "yes")
                    textBoxString.Text = null;
            }

            textBoxString.Text += num;
            prestr += num;
        }

        private void operate(Oper o)
        {
            switch (o)
            {
                //加号事件处理代码
                case Oper.Add:
                    {
                        if (iseq == "eq")
                        {

                            lastchar = "+";
                            textBoxString.Text = result.ToString() + "+";
                            pre = result;
                            flag = "+";
                            mude = null;
                            prestr = null;
                            alreq = "yes";
                            iseq = null;
                            break;
                        }
                        else
                        {
                            if (lastchar == "+" || lastchar == "=" || lastchar == "÷" || lastchar == "*")
                                break;
                            if (lastchar == "-")
                            {
                                lastchar = "+";
                                textBoxString.Text = textBoxString.Text.Remove(textBoxString.Text.Length - 1, 1) + "+";
                                flag = "+";
                                break;

                            }
                        }
                        if (mude != null)
                        {
                            if (mude == "*")
                            {
                                if (flag == "+")
                                {
                                    pre += (Convert.ToDecimal(premude) * Convert.ToDecimal(prestr));
                                }
                                else
                                {
                                    if (flag == "-")
                                    {
                                        pre -= (Convert.ToDecimal(premude) * Convert.ToDecimal(prestr));
                                    }
                                    else
                                        pre = (Convert.ToDecimal(premude) * Convert.ToDecimal(prestr));
                                }
                            }
                            else if (mude == "÷")
                            {
                                if (flag == "+")
                                {
                                    pre += (Convert.ToDecimal(premude) / Convert.ToDecimal(prestr));
                                }
                                else
                                {
                                    if (flag == "-")
                                    {
                                        pre -= (Convert.ToDecimal(premude) / Convert.ToDecimal(prestr));
                                    }
                                    else
                                        pre = (Convert.ToDecimal(premude) / Convert.ToDecimal(prestr));
                                }
                            }
                        }
                        else
                        {

                            if (flag != null)
                            {
                                if (flag == "+")
                                    pre += Convert.ToDecimal(prestr);
                                else if (flag == "-")
                                {
                                    pre -= Convert.ToDecimal(prestr);
                                }
                            }
                            else if (flag == null)
                            {
                                pre = Convert.ToDecimal(prestr);
                            }
                        }

                        mude = null;
                        prestr = null;
                        lastchar = "+";
                        textBoxString.Text += "+";
                        flag = "+";
                        break;
                    }
                //减号事件处理代码
                case Oper.Subtract:
                    {
                        if (iseq == "eq")
                        {

                            lastchar = "-";
                            textBoxString.Text = result.ToString() + "-";
                            pre = result;
                            flag = "-";
                            mude = null;
                            prestr = null;
                            alreq = "yes";
                            iseq = null;
                            break;
                        }
                        {
                            if (lastchar == "-" || lastchar == "=" || lastchar == "÷" || lastchar == "*")
                                break;
                            if (lastchar == "+")
                            {
                                lastchar = "-";
                                textBoxString.Text = textBoxString.Text.Remove(textBoxString.Text.Length - 1, 1) + "-";

                                flag = "-";
                                break;
                            }
                        }
                        if (mude != null)
                        {
                            if (mude == "*")
                            {
                                if (flag == "+")
                                {
                                    pre += (Convert.ToDecimal(premude) * Convert.ToDecimal(prestr));
                                }
                                else
                                {
                                    if (flag == "-")
                                    {
                                        pre -= (Convert.ToDecimal(premude) * Convert.ToDecimal(prestr));
                                    }
                                    else
                                        pre = (Convert.ToDecimal(premude) * Convert.ToDecimal(prestr));
                                }
                            }
                            if (mude == "÷")
                            {
                                if (flag == "+")
                                {
                                    pre += (Convert.ToDecimal(premude) / Convert.ToDecimal(prestr));
                                }
                                else
                                {
                                    if (flag == "-")
                                    {
                                        pre -= (Convert.ToDecimal(premude) / Convert.ToDecimal(prestr));
                                    }
                                    else
                                        pre = (Convert.ToDecimal(premude) / Convert.ToDecimal(prestr));
                                }
                            }
                        }
                        else
                        {
                            if (flag != null)
                            {
                                if (flag == "+")
                                    pre += Convert.ToDecimal(prestr);
                                else if (flag == "-")
                                {
                                    pre -= Convert.ToDecimal(prestr);
                                }
                            }
                            else if (flag == null)
                            {
                                pre = Convert.ToDecimal(prestr);
                            }
                        }

                        mude = null;
                        prestr = null;
                        lastchar = "-";
                        textBoxString.Text += "-";
                        flag = "-";
                        break;


                    }
                //等号事件处理代码
                case Oper.Equal:
                    {
                        if (lastchar == "+" || lastchar == "=" || lastchar == "-" || iseq == "eq" || lastchar == "÷" || lastchar == "*")
                        {

                            break;
                        }


                        if (mude != null)
                        {
                            if (mude == "*")
                            {
                                if (flag == "+")
                                {
                                    result = pre + (Convert.ToDecimal(premude) * Convert.ToDecimal(prestr));
                                }
                                else
                                {
                                    if (flag == "-")
                                    {
                                        result = pre - (Convert.ToDecimal(premude) * Convert.ToDecimal(prestr));
                                    }
                                    else
                                        result = (Convert.ToDecimal(premude) * Convert.ToDecimal(prestr));
                                }
                            }
                            else if (mude == "÷")
                            {
                                if (flag == "+")
                                {
                                    result = pre + (Convert.ToDecimal(premude) / Convert.ToDecimal(prestr));
                                }
                                else
                                {
                                    if (flag == "-")
                                    {
                                        result = pre - (Convert.ToDecimal(premude) / Convert.ToDecimal(prestr));
                                    }
                                    else
                                        result = (Convert.ToDecimal(premude) / Convert.ToDecimal(prestr));
                                }
                            }
                        }
                        else
                        {
                            if (flag != null)
                            {
                                if (flag == "+")
                                    result = pre + Convert.ToDecimal(prestr);
                                else if (flag == "-")
                                {
                                    result = pre - Convert.ToDecimal(prestr);
                                }
                            }
                            else if (flag == null)
                            {
                                result = Convert.ToDecimal(prestr);
                            }
                        }
                        mude = null;
                        prestr = null;
                        lastchar = "=";
                        textBoxString.Text += "=" + result.ToString();
                        flag = null;
                        iseq = "eq";
                        alreq = null;
                        break;


                    }
                //乘号事件处理代码
                case Oper.Multiply:
                    {

                        if (iseq == "eq")
                        {

                            lastchar = "*";
                            textBoxString.Text = result.ToString() + "*";
                            pre = result;
                            premude = result.ToString();
                            flag = null;
                            mude = "*";
                            prestr = null;
                            alreq = "yes";
                            iseq = null;
                            break;
                        }
                        {
                            if (lastchar == "+" || lastchar == "=" || lastchar == "*")
                                break;
                            if (lastchar == "÷")
                            {

                                lastchar = "*";
                                textBoxString.Text = textBoxString.Text.Remove(textBoxString.Text.Length - 1, 1) + "*";
                                mude = "*";
                                break;

                            }
                        }
                        if (mude != null)
                        {
                            if (mude == "*")
                                premude = ((Convert.ToDecimal(premude)) * (Convert.ToDecimal(prestr))).ToString();
                            else if (mude == "÷")
                            {
                                premude = ((Convert.ToDecimal(premude)) / (Convert.ToDecimal(prestr))).ToString();
                                mude = "*";
                            }

                        }

                        else
                        {
                            mude = "*";
                            premude = prestr;

                        }

                        prestr = null;
                        lastchar = "*";
                        textBoxString.Text += "*";
                        break;

                    }
                //除号事件处理代码
                case Oper.Divide:
                    {
                        if (iseq == "eq")
                        {

                            lastchar = "÷";
                            textBoxString.Text = result.ToString() + "÷";
                            pre = result;
                            premude = result.ToString();
                            flag = null;
                            mude = "÷";
                            prestr = null;
                            alreq = "yes";
                            iseq = null;
                            break;
                        }
                        {
                            if (lastchar == "+" || lastchar == "=" || lastchar == "÷")
                                break;
                            if (lastchar == "*")
                            {
                                lastchar = "÷";
                                textBoxString.Text = textBoxString.Text.Remove(textBoxString.Text.Length - 1, 1) + "÷";
                                mude = "÷";
                                break;

                            }
                        }
                        if (mude != null)
                        {
                            if (mude == "*")
                                premude = ((Convert.ToDecimal(premude)) * (Convert.ToDecimal(prestr))).ToString();
                            else
                                premude = ((Convert.ToDecimal(premude)) / (Convert.ToDecimal(prestr))).ToString();
                            mude = "÷";

                        }
                        else
                        {
                            mude = "÷";
                            premude = prestr;

                        }

                        prestr = null;
                        lastchar = "÷";
                        textBoxString.Text += "÷";
                        break;
                    }
            }
        }
    }
}
